import api from '../../service/index.js'
import wxService from '../../service/wx-service.js'

import {dealurl} from '../../utils/index.js'
Page({
  data: {
    swiper: [],
    news: [],
    category:[],
    hasMore: true,
  },
  init(){
    api.news({
      data: { recommend: '1',limit: '10', page: 1, order:'asc'},
      success:(res)=>{
       
        var hasMore = this.hasMore
        if (res.data.page.currPage >= res.data.page.totalPage){
        hasMore = false;
        }
        this.setData({
          news: res.data.page,
          hasMore: hasMore
        })
        wx.stopPullDownRefresh();

      },
      fail:(err)=>{

      },
    })

    api.category({
      data: { limit: '10', page: 1, order: 'asc' },
      success: (res) => {
        this.setData({
          category: res.data.page,
        })
        wx.stopPullDownRefresh();

      },
      fail: (err) => {

      },
    })
    api.carouselList({
      data: { limit: '10', page: 1, order: 'asc' ,status:'1'},
      success: (res) => {
        this.setData({
          swiper: res.data.page.list||[],
        })
        wx.stopPullDownRefresh();

      },
      fail: (err) => {

      },
    })
  },
  onLoad () {
    this.init()
    wx.checkSession({
      success:function(res){
        console.log("success")
      },
      fail:function(res){
        console.log("checkSession fail")

        wx.login({
          success:function(res){
            wx.setStorageSync("userInfo", res)
            console.log(res)
          },
          fail:function(res){
            console.log("login fail..")

          }
        })
      }
    })
  },
  loadMore(){
    let currentPage = this.data.news.currPage;
    if (currentPage >= this.data.news.totalPage) {
        this.setData({
            hasMore: false,
        });
        return;
    }

    api.news({
      data: { recommend:'1',limit: '10', order: 'asc',page:++currentPage},
      success:(res)=>{
        let newDatas = res.data.page;
        newDatas.list = [...this.data.news.list,...newDatas.list];
        this.setData({
          news:newDatas
        })
      },
      fail:(err)=>{

      },
    })
  },
  toCarousel(event) {
    wx.navigateTo({
      url: '../carousel/carousel?id=' + event.currentTarget.dataset.id,
        success: (res) => {},
        fail: (err) => {
            console.log(err)
        }
    });
  },
  toArticle(event){
    wx.navigateTo({
      url: '../article/article?id=' + event.currentTarget.dataset.id  ,
        success: (res) => {},
        fail: (err) => {
            console.log(err)
        }
    });
  },
  toTopic(event){
    wx.navigateTo({
      url: '../topic/topic?templateid=' + event.currentTarget.dataset.templateid +'&id='+event.currentTarget.dataset.id,
        success: (res) => {},
        fail: (err) => {
            console.log(err)
        }
    });
  },
  toVideo(event){
    wx.navigateTo({
      url: '../video/video?id=' + event.currentTarget.dataset.id ,
        success: (res) => {},
        fail: (err) => {
            console.log(err)
        }
    });
  },
  onPullDownRefresh () {
    this.init()
  },
  onReachBottom () {
    this.loadMore()
  },
  toSwitch(event){
    let id = event.currentTarget.dataset.id 
    if(!id){
      return;
    }

    let type = event.currentTarget.dataset.type||0

    switch (type){
      case 0: this.toArticle(event);break;
      case 1: this.toCarousel(event); break;
      case 2: this.toVideo(event); break;
    }
  },
  imgerr(e){
    console.error(e)
  }
})
